import os

import pandas as pd


# 报存到csv文件
def excle_to_csv(old_file, new_path):
    data_xls = pd.read_excel(old_file, index_col=0)
    data_xls.to_csv(new_path, encoding='utf-8')


def filter_xlsx_xls(excel_path, csv_path):
    files = os.listdir(excel_path)  # 列出xls文件夹下的所有文件
    for i in files:
        kname = os.path.splitext(i)[1]  # 分离文件名与扩展名，返回(f_name, f_extension)元组
        if kname in ['.xls', '.xlsx']:  # 判定扩展名是否为xls, xlsx,屏蔽其它文件
            filename = os.path.splitext(i)[0].split('/')
            file_path = csv_path + filename[len(filename) - 1] + '.csv'
            excle_to_csv(excel_path + i, file_path)
            save_mongodb(file_path, filename[len(filename) - 1])


# 报存到数据库
def save_mongodb(file, dict_name):
    import pymongo
    import csv

    client = pymongo.MongoClient(host='39.106.114.82', port=27017)
    # dblist = client.list_database_names()
    # print(dblist)
    db = client.mydb
    my_set = db[dict_name]

    with open(file, 'r', encoding='utf-8')as csvfile:
        reader = csv.DictReader(csvfile)
        # 创建一个counts计数一下 看自己一共添加了了多少条数据
        counts = 0
        for each in reader:
            my_set.insert(each)
            counts += 1
            print('成功添加了' + str(counts) + '条数据 ')


if __name__ == '__main__':
    filter_xlsx_xls("./excel/", "./csv/")
